#include "facility_manager.h"
#include "welcome.h"
#include "facility.h"
#include "record.h"
#include "file.h"
#include "user_reservation.h"
// 创建场地负责人
FacilityManager create_facility_manager(int id, char* name, int num_facilities, Facility* facilities[MAX_FACILITIES], char* username, char* password, long long phone) {
    FacilityManager facility_manager;
    facility_manager.id = id;
    facility_manager.num_facilities = num_facilities;
    strncpy_s(facility_manager.name, MAX_NAME_LENGTH, name, MAX_NAME_LENGTH - 1);
    facility_manager.name[MAX_USERNAME - 1] = '\0';
    facility_manager.phone = phone;
    strncpy_s(facility_manager.username, MAX_NAME_LENGTH, username, MAX_NAME_LENGTH - 1);
    facility_manager.username[MAX_USERNAME - 1] = '\0';
    strncpy_s(facility_manager.password, MAX_NAME_LENGTH, password, MAX_NAME_LENGTH - 1);
    facility_manager.password[MAX_USERNAME - 1] = '\0';
    // 复制数组
    if (num_facilities > MAX_FACILITIES) {
        printf("Warning: num_facilities exceeds MAX_FACILITIES. Truncating...\n");
        num_facilities = MAX_FACILITIES;
    }
    for (int i = 0; i < num_facilities; i++) {
        facility_manager.facilities[i] = facilities[i];
    }
    if (num_facilities < MAX_FACILITIES)   facility_manager.facilities[num_facilities] = NULL;
    return facility_manager;
}
// 初始化场馆负责人链表
void init_FMList(FMList* list) {
    list->num = 0;
    list->head = (FMNode*)malloc(sizeof(FMNode));
    if (list->head == NULL) {
        printf("内存分配失败！\n");
        return;
    }
    list->head->next = NULL;
}
// 创建场馆负责人节点
FMNode* create_FMNode(FacilityManager* manager) {
    FMNode* new_node = (FMNode*)malloc(sizeof(FMNode));
    if (new_node == NULL) {
        printf("内存分配失败！\n");
        return NULL;
    }
    new_node->facility_manager = *manager;
    new_node->next = NULL;
    return new_node;
}
// 添加场馆负责人
void add_facility_manager(FMList* list, FacilityManager* manager) {
    FMNode* new_node = create_FMNode(manager);
    FMNode* temp = list->head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = new_node;
    list->num++;
}
// 删除场馆负责人节点
int delete_facility_manager(FMList* list, int manager_id) {
    if (list->head->next == NULL) {
        printf("场馆负责人链表为空！\n");
        return -1;
    }
    FMNode* cur_node = list->head->next;
    FMNode* pre_node = list->head;
    // 查找要删除的场馆负责人
    while (cur_node != NULL && cur_node->facility_manager.id != manager_id) {
        pre_node = cur_node;
        cur_node = cur_node->next;
    }
    // 如果没有找到该场馆负责人
    if (cur_node == NULL) {
        printf("没有找到指定ID的场馆负责人！\n");
        return -1;
    }
    // 删除节点
    pre_node->next = cur_node->next;
    free(cur_node);
    list->num--;
    return 0;
}
// 查找场馆负责人节点
FacilityManager* find_facility_manager(FMList* list, int manager_id) {
    FMNode* temp = list->head->next;
    while (temp != NULL) {
        if (temp->facility_manager.id == manager_id) {
            return &temp->facility_manager;
        }
        temp = temp->next;
    }
    return NULL;  // 没有找到场馆负责人
}
// 查找场馆负责人(根据账号)
FacilityManager* find_facility_manager_by_username(FMList* list, char* username) {
    FMNode* temp = list->head->next;
    while (temp != NULL) {
        if (strncmp(temp->facility_manager.username, username, MAX_USERNAME) == 0) {
            return &temp->facility_manager;
        }
        temp = temp->next;
    }
    return NULL;
}
// 打印链表中的所有场馆负责人
void print_facility_managers(FMList* list) {
    if (list->head->next == NULL) {
        printf("场馆负责人链表为空！\n");
        return;
    }
    FMNode* temp = list->head->next;
    printf("场馆负责人列表：\n");
    while (temp != NULL) {
        printf("ID: %d, 姓名: %s, 电话: %lld, 管理场地数量: %d, 用户名: %s\n",
            temp->facility_manager.id,
            temp->facility_manager.name,
            temp->facility_manager.phone,
            temp->facility_manager.num_facilities,
            temp->facility_manager.username);
        temp = temp->next;
    }
}
// 读取判断
char judge_manager() {
    while (true) {
        printf(" 请选择：");
        char c = getchar();
        clear_input_buffer();
        if (c == '1' || c == '2' || c == '3' ||c =='4'||c=='5'||c=='6'|| c == 'q' || c == 'b')  return c;
        else {
            printf(" 输入有误，请重新输入\n");
        }
    }
}
// 场地负责人操作
void manager_operation(char c, System* sys,int facility_manager_id) {
    switch (c) {
    case'1':    // 查看所管场地
        show_manager_facility(sys, facility_manager_id);
        printf(" ");
        system("pause");
        // 返回登录后的界面
        system("cls");
        manager();
        char ch1 = judge_manager();
        manager_operation(ch1, sys, facility_manager_id);
        break;
    case'2':    // 添加场地使用记录
        add_usage_log(sys, facility_manager_id);
        // 返回登录后的界面
        system("cls");
        manager();
        char ch2 = judge_manager();
        manager_operation(ch2, sys, facility_manager_id);
        break;
    case'3':    // 密码维护
        password_maintenance(sys,facility_manager_id);
        // 返回登录后的界面
        system("cls");
        manager();
        char ch3 = judge_manager();
        manager_operation(ch3, sys, facility_manager_id);
        break;
    case'4':    //信息查找
        system("cls");
        look_for(*sys);
        char ch4;
        while (true) {
            printf(" 请选择：");
            ch4 = getchar();
            clear_input_buffer();
            if (ch4 == '1' || ch4 == '2' || ch4 == '3' || ch4 == '4' || ch4 == 'q' || ch4 == 'b')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (ch4) {
        case'1':    //查询预订信息
            system("cls");
            look_for_res(*sys);
            char cha1;
            while (true) {
                printf(" 请选择：");
                cha1 = getchar();
                clear_input_buffer();
                if (cha1 == '1' || cha1 == '2' || cha1 == '3' || cha1 == '4' || cha1 == '5'||cha1=='b'||cha1=='q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha1) {
            case'1':    // 查询预订信息（姓名 模糊）
                search_reservation_by_name(*sys);
                system("pause");
                break;
            case'2':    // 查询预订信息（电话）
                search_reservation_by_phone(*sys);
                system("pause");
                break;
            case'3':    // 查询预订信息（日期）
                search_reservation_by_date(*sys);
                system("pause");
                break;  
            case'4':    // 查询预订信息（姓名+日期）
                search_reservation_by_name_date(*sys);
                system("pause");
                break;
            case'5':    // 查询预订信息（电话+日期）
                search_reservation_by_phone_date(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
            }
            break;
        case'2':    //查询使用记录信息 
            system("cls");
            look_for_record(*sys);
            char cha2;
            while (true) {
                printf(" 请选择：");
                cha2 = getchar();
                clear_input_buffer();
                if (cha2 == '1' || cha2 == '2' || cha2 == '3' || cha2 == '4' || cha2 == '5' || cha2 == 'b' || cha2 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha2) {
            case'1':    // 查询使用记录 用户电话
                search_record_by_phone(*sys);
                system("pause");
                break;
            case'2':    // 查询使用记录 场地ID
                search_record_by_facility_id(*sys);
                system("pause");
                break;
            case'3':    // 查询使用记录 日期
                search_record_by_date(*sys);
                system("pause");
                break;
            case'4':    // 查询预订信息（姓名+日期）
                search_record_by_phone_date(*sys);
                system("pause");
                break;
            case'5':    // 查询预订信息（电话+日期）
                search_record_by_facility_id_date(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
            }
            break;
        case'3':    //查找场地信息
            system("cls");
            look_for_facility(*sys);
            break;
        case'4':    //查找用户信息
            system("cls");
            look_for_user(*sys);
            char cha4;
            while (true) {
                printf(" 请选择：");
                cha4 = getchar();
                clear_input_buffer();
                if (cha4 == '1' || cha4 == '2' || cha4 == '3' || cha4 == '4' || cha4 == 'b' || cha4 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha4) {
            case'1':    // 查询用户信息 昵称 模糊
                search_user_by_nickname(*sys);
                system("pause");
                break;
            case'2':    // 查询用户信息 电话
                search_user_by_phone(*sys);
                system("pause");
                break;
            case'3':    // 查询用户信息 身份证号
                search_user_by_id_num(*sys);
                system("pause");
                break;
            case'4':    // 查询用户信息 真实姓名 模糊
                search_user_by_name(*sys);
                system("pause");
                break;
            case'b':    
                break;
            case'q':
                exit(0);
            }
            break;
        case'b':
            break;
        case'q':
            exit(0);
        }
        // 返回登录后的界面
        system("cls");
        manager();
        ch4 = judge_manager();
        manager_operation(ch4, sys, facility_manager_id);
        break;
    case'5':    //信息排序
        system("cls");
        my_sort(*sys);
        char ch5;
        char sort5;
        while (true) {
            printf(" 请选择：");
            sort5 = getchar();
            clear_input_buffer();
            if (sort5 == '1' || sort5 == '2' || sort5 == '3' || sort5 == '4' || sort5 == 'q' ||
                sort5 == '5' || sort5 == '6' || sort5 == '7' || sort5 == 'b')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (sort5) {
        case'1':    //  查看预订消息（单项排序）
            system("cls");
            sort_reservation();
            char s1;
            while (true) {
                printf(" 请选择：");
                s1 = getchar();
                clear_input_buffer();
                if (s1 == '1' || s1 == '2' || s1 == '3' || s1 == '4' || s1 == 'q' || s1 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s1) {
            case'1':    //Order by date
                sort_res_order_by_date(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'2':    //Order by id
                sort_res_order_by_id(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'3':    //Order by facility_id
                sort_res_order_by_facility_id(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'4':    //Order by name
                sort_res_order_by_name(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'2':    //  查看预订消息（组合排序）
            system("cls");
            sort_res();
            int arr2[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr2[0]);
                clear_input_buffer();
                if (arr2[0] < 1 && arr2[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr2[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr2[i+1]);
                    clear_input_buffer();
                    if (arr2[i+1] >= 1 && arr2[i+1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr2[0]; i++) {
                switch (arr2[arr2[0]-i]) {
                case 1:
                    sort_res_order_by_date(&sys->reservation_link);
                    break;
                case 2:
                    sort_res_order_by_id(&sys->reservation_link);
                    break;
                case 3:
                    sort_res_order_by_facility_id(&sys->reservation_link);
                    break;
                case 4:
                    sort_res_order_by_name(&sys->reservation_link);
                    break;
                }

            }
            printf_res(sys->reservation_link);
            system("pause");
            break;
        case'3':    //  查看使用记录（单项排序）
            system("cls");
            sort_record();
            //print_records(&sys->record_link);
            char s3;
            while (true) {
                printf(" 请选择：");
                s3 = getchar();
                clear_input_buffer();
                if (s3 == '1' || s3 == '2' || s3 == '3' || s3 == '4' || s3 == 'q' || s3 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s3) {
            case'1':    //Order by date
                sort_record_order_by_date(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'2':    //Order by id
                sort_record_order_by_id(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'3':    //Order by facility_id
                sort_record_order_by_facility_id(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'4':    //Order by name
                sort_record_order_by_name(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'4':    //  查看使用记录（组合排序）
            system("cls");
            sort_records();
            int arr4[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr4[0]);
                clear_input_buffer();
                if (arr4[0] < 1 && arr4[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr4[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr4[i + 1]);
                    clear_input_buffer();
                    if (arr4[i + 1] >= 1 && arr4[i + 1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr4[0]; i++) {
                switch (arr4[arr4[0]-i]) {
                case 1:
                    sort_record_order_by_date(&sys->record_link);
                    break;
                case 2:
                    sort_record_order_by_id(&sys->record_link);
                    break;
                case 3:
                    sort_record_order_by_facility_id(&sys->record_link);
                    break;
                case 4:
                    sort_record_order_by_name(&sys->record_link);
                    break;
                }

            }
            print_records(&sys->record_link);
            system("pause");
            break;
        case'5':    //  查看场地（单项排序）
            system("cls");
            sort_facility();
            char s5;
            while (true) {
                printf(" 请选择：");
                s5 = getchar();
                clear_input_buffer();
                if (s5 == '1' || s5 == '2' || s5 == '3' || s5 == '4' || s5 == 'q' || s5 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s5) {
            case'1':    //Order by id
                sort_facility_order_by_id(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'2':    //Order by area
                sort_facility_order_by_area(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'3':    //Order by type
                sort_facility_order_by_type(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'4':    //Order by fee
                sort_facility_order_by_fee(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'6':    //  查看场地（组合排序）
            system("cls");
            sort_fac();
            int arr6[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr6[0]);
                clear_input_buffer();
                if (arr6[0] < 1 && arr6[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr6[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr6[i + 1]);
                    clear_input_buffer();
                    if (arr6[i + 1] >= 1 && arr6[i + 1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr6[0]; i++) {
                switch (arr6[arr6[0]-i]) {
                case 1 :
                    sort_facility_order_by_id(&sys->facility_link);
                    break;
                case 2 :
                    sort_facility_order_by_area(&sys->facility_link);
                    break;
                case 3 :
                    sort_facility_order_by_type(&sys->facility_link);
                    break;
                case 4 :
                    sort_facility_order_by_fee(&sys->facility_link);
                    break;
                }

            }
            print_facilities(&sys->facility_link);
            system("pause");
            break;
            break;
        case'7':    //  查看用户（单项排序）
            system("cls");
            sort_user();
            char s7;
            while (true) {
                printf(" 请选择：");
                s7 = getchar();
                clear_input_buffer();
                if (s7 == '1' || s7 == '2' || s7 == '3' || s7 == 'q' || s7 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s7) {
            case'1':    //Order by name
                sort_user_order_by_name(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'2':    //Order by phone
                sort_user_order_by_phone(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'3':    //Order by name + phone
                sort_user_order_by_name(&sys->user_link);
                sort_user_order_by_phone(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'b':
            break;
        case'q':
            exit(0);
            break;
        }
        // 返回登录后的界面
        system("cls");
        manager();
        ch5 = judge_manager();
        manager_operation(ch5, sys, facility_manager_id);
        break;
    case'6':    //信息统计
        system("cls");
        statistic(*sys);
        char ch6;
        char statistic6;
        while (true) {
            printf(" 请选择：");
            statistic6 = getchar();
            clear_input_buffer();
            if (statistic6 == '1' || statistic6 == '2' || statistic6 == '3' || statistic6 == '4'  ||
                statistic6 == '5' || statistic6 == '6' || statistic6 == 'b' || statistic6 == 'q')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (statistic6) {
        case'1':    // 预订消息统计
            system("cls");
            reservation_statistic(*sys);
            char sta1;
            while (true) {
                printf(" 请选择：");
                sta1 = getchar();
                clear_input_buffer();
                if (sta1 == '1' || sta1 == '2' || sta1 == '3' || sta1 == '4' || sta1 == 'b' || sta1 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta1) {
            case'1':    
                statistic_res_by_user(*sys);
                system("pause");
                break;
            case'2':
                statistic_res_by_facility_type(*sys);
                system("pause");
                break;
            case'3':
                statistic_res_by_user_and_facility_type(*sys);
                system("pause");
                break;
            case'4':
                statistic_res_by_date_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'2':    // 使用记录统计
            system("cls");
            record_statistic(*sys);
            char sta2;
            while (true) {
                printf(" 请选择：");
                sta2 = getchar();
                clear_input_buffer();
                if (sta2 == '1' || sta2 == '2' || sta2 == '3' || sta2 == '4' ||
                    sta2 == '5' ||sta2 == 'b' || sta2 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta2) {
            case'1':    //根据用户
                statistic_record_by_user(*sys);
                system("pause");
                break;
            case'2':    //根据场地
                statistic_record_by_facility(*sys);
                system("pause");
                break;
            case'3':    //根据日期
                statistic_record_by_date(*sys);
                system("pause");
                break;
            case'4':    //根据用户+场地
                statistic_record_by_user_facility(*sys);
                system("pause");
                break;
            case'5':    //根据日期范围
                statistic_record_by_date_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'3':    // 场地信息统计
            system("cls");
            facility_statistic(*sys);
            char sta3;
            while (true) {
                printf(" 请选择：");
                sta3 = getchar();
                clear_input_buffer();
                if (sta3 == '1' || sta3 == '2' || sta3 == '3' || sta3 == '4' || sta3 == '5'||
                    sta3 == '6' || sta3 == '7' ||sta3 == 'b' || sta3 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta3) {
            case'1':    //统计场地 根据类型
                statistic_facility_by_type(*sys);
                system("pause");
                break;
            case'2':    //统计场地 根据价格
                statistic_facility_by_price(*sys);
                system("pause");
                break;
            case'3':    //统计场地 根据面积
                statistic_facility_by_area(*sys);
                system("pause");
                break;
            case'4':    //统计场地 根据类型+价格
                statistic_facility_by_type_price(*sys);
                system("pause");
                break;
            case'5':    //统计场地 根据类型+面积
                statistic_facility_by_type_area(*sys);
                system("pause");
                break;
            case'6':    //统计场地 根据面积范围
                statistic_facility_by_area_range(*sys);
                system("pause");
                break;
            case'7':    //统计场地 根据价格范围
                statistic_facility_by_price_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'4':    // 预约信息总统计
            res_all_statistic(*sys);
            system("pause");
            break;
        case'5':    // 使用记录总统计
            statistic_all_record(*sys);
            system("pause");
            break;
        case'6':    // 未到场的预约消息统计
            res_default_statistic(*sys);
            system("pause");
            break;
        case'b':
            break;
        case'q':
            exit(0);
            break;
        }
        // 返回登录后的界面
        system("cls");
        manager();
        ch6 = judge_manager();
        manager_operation(ch6, sys, facility_manager_id);
        break;
    case'b':    // 返回 系统界面
        break;
    case'q':    // 退出
        exit(0);
        break;
    }
}
// 密码维护
void password_maintenance(System* sys,int facility_manager_id) {
    system("cls");
    printf(" -------------------------------------------------------------------------------\n");
    printf("\t\t\t   欢迎来到运动场馆管理系统    \t\t\t\n");
    printf("\t\t\t\t场地负责人界面    \t\t\t\n");
    printf(" -------------------------------------------------------------------------------\n");
    printf("\t\t\t修改密码 \t\t请按 1 \n");
    printf("\t\t\t密码找回设置 \t\t请按 2 \n");
    printf(" -------------------------------------------------------------------------------\n");
    printf("\t\t\t返回系统界面\t\t请按 b \n");
    printf("\t\t\t退出 \t\t\t请按 q \n");
    printf(" -------------------------------------------------------------------------------\n");
    char c;
    while (true) {
        printf(" 请选择：");
        c = getchar();
        clear_input_buffer();
        if (c == '1' || c == '2'|| c == 'q' || c == 'b')  break;
        else {
            printf(" 输入有误，请重新输入\n");
        }
    }
    switch (c) {
    case'1':    //修改密码
        change_password(sys,facility_manager_id);
        break;
    case'2':    //问题设置
        
        break;
    case'b':    // 返回 系统界面
        break;
    case'q':    // 退出
        exit(0);
        break;
    }
}
// 修改密码
void change_password(System* sys,int facility_manager_id) {
    FacilityManager* facility_manager = find_facility_manager(&sys->facility_manager_link, facility_manager_id);
    char old_password[MAX_PASSWORD];
    char new_password[MAX_PASSWORD];
    while (true) {
        printf(" 请输入原密码：");
        scanf_s("%s", old_password, MAX_PASSWORD);
        clear_input_buffer();
        if (strncmp(facility_manager->password, old_password, MAX_PASSWORD) == 0) {
            while (true) {
                printf(" 请输入新密码：");
                scanf_s("%s", new_password, MAX_PASSWORD);
                clear_input_buffer();
                printf(" 新密码已输入，是否确认修改 Y/N:");
                char c = getchar();
                clear_input_buffer();
                strcpy_s(facility_manager->password,20,new_password);
                write_facility_manager_file(*sys,"..\\src\\facility_manager.txt");
                if (c == 'Y') {
                    printf(" 修改成功\n");
                    printf(" 三秒后自动返回...");
                    Sleep(3000);
                    return;
                }
                else {
                    printf(" 是否继续修改密码 Y/N:");
                    c = getchar();
                    clear_input_buffer();
                    if (c == 'Y') {
                        continue;
                    }
                    else {
                        return;
                    }
                }
            }
        }
        else {
            printf(" 密码不正确！");
            printf(" 是否继续修改密码 Y/N:");
            char c = getchar();
            clear_input_buffer();
            if (c == 'Y') {
                continue;
            }
            else {
                return;
            }
        }
    }
    
}
// 开具场地使用记录
void add_usage_log(System* sys,int facility_manager_id) {
    system("cls");
    printf(" -----------------------------------------------------------------------------------\n");
    printf("\t\t\t     欢迎来到运动场馆管理系统    \t\t\t\n");
    printf("\t\t\t\t  场地负责人界面    \t\t\t\n");
    printf(" -----------------------------------------------------------------------------------\n");
    printf("\t\t\t  未预约/延长使用时间 \t\t请按 1 \n");
    printf("\t\t\t  已预约（仅选择场地类型） \t请按 2 \n");
    printf("\t\t\t  已预约（选择具体场地） \t请按 3 \n");
    printf(" -----------------------------------------------------------------------------------\n");
    printf("\t\t\t  返回系统界面\t\t请按 b \n");
    printf("\t\t\t  退出 \t\t\t请按 q \n");
    printf(" -----------------------------------------------------------------------------------\n");
    char c;
    while (true) {
        printf(" 请选择：");
        c = getchar();
        clear_input_buffer();
        if (c == '1' || c == '2' || c == '3' || c == 'q' || c == 'b')  break;
        else {
            printf(" 输入有误，请重新输入\n");
        }
    }
    switch (c) {
    case'1':    //未预约 延长使用时间    OK
        unreserved(sys, facility_manager_id);
        break;
    case'2':    //已预约(仅选择场地类型) OK
        only_type_reserved(sys, facility_manager_id);
        break;
    case'3':    //已预约(选择具体场地)   
        //have_reserved();
        have_reserved(sys, facility_manager_id);
        break;
    case'b':    // 返回 系统界面
        break;
    case'q':    // 退出
        exit(0);
        break;
    }
}
// 未预约 延长使用时间
void unreserved(System* sys,int facility_managar_id) {
    FacilityManager facility_manager = *find_facility_manager(&sys->facility_manager_link, facility_managar_id);
    User *user;
    char phone[50];
    int facility_id;
    Facility *facility;
    char year[5] = { '\0' }, month[3] = { '\0' }, day[3] = {'\0'};
    char begin_time[6], end_time[6];
    system("cls");
    printf(" -----------------------------------------------------------------------------------\n");
    printf("\t\t\t     欢迎来到运动场馆管理系统    \t\t\t\n");
    printf("\t\t\t        添加使用记录    \t\t\t\n");
    printf(" -----------------------------------------------------------------------------------\n");
    while (true) {
        printf(" 请输入用户电话号：");
        scanf_s("%s", phone, 50);
        clear_input_buffer();
        User_node* user_node = find_phone_data(sys->user_link, phone);
        if (user_node == NULL) {
            printf(" 输入错误，是否重新输入 Y/N:");
            char c = getchar();
            clear_input_buffer();
            if (c == 'Y') {
                continue;
            }
            else {
                break;
            }
        }
        else {
            user = &user_node->user;
            printf(" 篮球场(1) 羽毛球场(2) 足球场(3) 台球场(4) 乒乓球场(5) 排球场(6)\n");
            while (true) {
                printf(" 请输入场地类型代号数字（eg: 1 代表 篮球场）:");
                int type;
                scanf_s("%d", &type);
                clear_input_buffer();
                if (type >= 1 && type <= 5) {
                    printf(" 管理场地\t当日时间人流表\n");
                    int index = 0;
                    for (int i = 0; i < facility_manager.num_facilities; i++) {
                        if (facility_manager.facilities[i]->type == type) {
                            index++;
                            printf(" %d\t", facility_manager.facilities[i]->id);
                            printf(" %s\t", facility_manager.facilities[i]->name);
                            printf(" 场地价格（半小时）:%.2f元\n", facility_manager.facilities[i]->price_per_half_hour);
                            printf(" 场地最多容纳人数:%d\t", facility_manager.facilities[i]->max_person_num);
                            printf(" 营业时间： %s\n", facility_manager.facilities[i]->open_time);
                            /*   printf(" 8点  9点  10点  11点  12点  13点  14点  15点  16点  17点  18点  19点  20点  21点\n");
                            for (int j = 0; j < 28; j++) {
                                printf("%d  ",facility_manager.facilities[i]->time_list.head->next->day_time[j]);
                            }*/
                            printf(" 08:00   09:00   10:00   11:00   12:00   13:00   14:00\n");
                            for (int j = 0; j < 14; j++) {
                                printf("   %d", facility_manager.facilities[i]->time_list.head->next->t.day_time[j]);
                            }
                            printf("\n 15:00   16:00   17:00   18:00   19:00   20:00   21:00\n");
                            for (int j = 14; j < 28; j++) {
                                printf("   %d", facility_manager.facilities[i]->time_list.head->next->t.day_time[j]);
                            }
                            printf("\n");
                            printf("\n");
                       }
                    }
                    if (index == 0) {
                        printf(" 无匹配的场地 请找其他负责人\n");
                        system("pause");
                        return;
                    }
                    break;
                }
                else {
                    printf(" 输入不合法\n");
                    continue;
                }
            }
            while (true) {
                int index = 0;
                printf(" 请输入开始时间(eg 08:00)：");
                scanf_s("%s", begin_time, 6);
                clear_input_buffer();
                int begin = index_time(begin_time);
                begin_time[2] = ':';
                printf(" 请输入结束时间(eg 09:30)：");
                scanf_s("%s", end_time, 6);
                clear_input_buffer();
                int end = index_time(end_time);
                end_time[2] = ':';
                if (begin < end && begin >= 0 && end <= 27) {
                    //printf("%d %d", begin, end);
                    while (true) {
                        printf(" 请选择场地(ID)：");
                        scanf_s("%d", &facility_id);
                        clear_input_buffer();
                        Facility* facility = find_facility(&sys->facility_link, facility_id);
                        if (facility == NULL) {     // 小问题：可能选择其他场地负责人的场地
                            printf(" 无此场地,请重新输入。\n");
                            continue;
                        }
                        else {
                            for (int i = begin; i < end; i++) {
                                int cur = facility->time_list.head->next->t.day_time[i] + 1;
                                if (cur > facility->max_person_num) {
                                    printf(" 时间冲突，请重新选择\n");
                                    break;
                                }
                            }
                            // 人数+1
                            for (int i = begin; i < end; i++) {
                                facility->time_list.head->next->t.day_time[i]++;
                            }
                            index = 1;
                            break;
                        }
                    }
                }
                else {
                    printf(" 时间不合法，请重新输入\n");
                }
                if (index)   break;
            }

            // 创建记录
            facility = find_facility(&sys->facility_link, facility_id);
            time_t now = 0;
            time(&now);
            struct tm to_tm = *localtime(&now);
            to_str(to_tm.tm_year + 1900, year);
            to_str(to_tm.tm_mon + 1,month);
            if (month[1] == '\0') {
                month[1] = month[0];
                month[0] = '0';
            }
            to_str(to_tm.tm_mday,day);
            if (day[1] == '\0') {
                day[1] = day[0];
                day[0] = '0';
            }
            Record record = create_record(user, facility, year, month, day, begin_time, end_time, false);
            // 写入文件（保存）
            add_record(&sys->record_link, &record);
            write_record_file(sys, "..\\src\\record.txt");
            printf("记录成功！\n");
            system("pause");
            break;
        }
    }

}
// 计算时间标签
int index_time(char* time) {
    int a, b,cnt = 0;
    char* context;
    char* temp = strtok_s(time, ":",&context);
    while (temp)
    {
        //printf("%s\n", temp);
        cnt++;
        if (cnt == 1) {
            if (atoi(temp) == 0)   return -1;
            a = atoi(temp) - 8;
        }
        if (cnt == 2)  b = atoi(temp) / 30;
        temp = strtok_s(NULL, ":",&context);
    }
    return a * 2 + b;
}
// 翻转字符串
void reverse(char* str, int len) {
    int start = 0, end = len - 1;
    while (start < end) {
        // swap 前后字符
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}
// 数字转str
void to_str(int val,char* str) {
    int i = 0;
    int isNegative = 0; // 符号位
    if (val < 0) {
        isNegative = 1;
        val = -1 * val;
    }
    else if (val == 0) {
        str[i++] = '0';
        str[i] = '\0';
        return;
    }
    while (val != 0) {
        int digit = val % 10;
        str[i++] = digit + '0';
        val = val / 10;
    }
    // 添加负号
    if (isNegative)  str[i++] = '-';
    // 翻转字符串
    reverse(str, i);
}
// 查看管理场地
void show_manager_facility(System* sys,int facility_manager_id) {
    FacilityManager* facility_manager = find_facility_manager(&sys->facility_manager_link, facility_manager_id);
    for (int i = 0; i < facility_manager->num_facilities; i++) {
        printf(" ID:%d\t", facility_manager->facilities[i]->id);
        printf(" Name:%s\t", facility_manager->facilities[i]->name);
        print_facility_type(facility_manager->facilities[i]->type);
        printf(" \t场地面积：%d平方米\n", facility_manager->facilities[i]->area);
        printf(" 场地价格(半小时):%.2f元\t", facility_manager->facilities[i]->price_per_half_hour);
        printf(" 场地最多容纳人数:%d\t", facility_manager->facilities[i]->max_person_num);
        printf(" 营业时间： %s\n", facility_manager->facilities[i]->open_time);
    }
}
// 判断时间是否合法
bool judge_time(System* sys,char* start_time, char* end_time, int facility_id,int year,int month,int day) {
    int start, end;
    start = index_time(start_time);
    end = index_time(end_time);
    if (end > start && end < 28 && start>0) {
        if (facility_id == 0) {
            return true;
        }
        else {
            Facility* facility = find_facility(&sys->facility_link, facility_id);
            int max = facility->max_person_num;
            TNode* t_node = facility->time_list.head->next;
            t_node = t_node->next;
            while (t_node != NULL) {
                if (t_node->t.day == day && t_node->t.month == month && t_node->t.year == year) {
                    for (int i = start; i < end; i++) {
                        if (t_node->t.day_time[i] == max) {
                            printf("时间段场馆满员\n");
                            system("pause");
                            return false;
                        }
                    }
                    for (int i = start; i < end; i++) {
                        t_node->t.day_time[i]++;
                    }
                }
                else {
                    t_node = t_node->next;
                }
            }
        }
        
    }
    else {
        printf("时间不合法\n");
        system("pause");
        return false;
    }
    return true;
}
// 已预约
void have_reserved(System* sys, int facility_manager_id) {
    print_res_about_facility_manager(sys,facility_manager_id);
    printf(" 请输入对应的预订ID：");
    while (true) {
        int res_id = 0;
        scanf_s("%d", &res_id);
        clear_input_buffer();
        User_res* user_res = find_res(&sys->reservation_link, res_id);
        if (user_res == NULL) {     // 小问题：可能选择其他预订
            printf(" 请重新输入ID:");
        }
        else {
            user_res->flag = 1;
            save_res_data(&sys->reservation_link);  //保存修改
            User_node* user_node = find_phone_data(sys->user_link, user_res->phone);
            Facility* facility = find_facility(&sys->facility_link, user_res->num);
            Record record = create_record(&user_node->user, facility, user_res->year,
                user_res->month, user_res->day, user_res->start_time, user_res->end_time, false);
            add_record(&sys->record_link, &record);
            write_record_file(sys,"..\\src\\record.txt");    //保存
            printf(" 登记成功\n");
            printf(" ");
            system("pause");
            break;
        }
    }
   
}
// 打印场地管理员相关的预订信息
void print_res_about_facility_manager(System* sys, int facility_manager_id) {
    Res_node* cur_node = sys->reservation_link.head;
    while (cur_node != NULL) {
        int facility_id = cur_node->user_res.num;
        if (cur_node->user_res.num == 0) {
            cur_node = cur_node->next;
            continue;
        }
        Facility facility = *find_facility(&sys->facility_link, facility_id);
        FacilityManager facility_manager = *find_facility_manager(&sys->facility_manager_link, facility.manager_id);
        if (facility_manager.id == facility_manager_id&&cur_node->user_res.flag==false) {
            printf("%s\t%s\t%s\t%d\t%d\t",
                cur_node->user_res.name,
                cur_node->user_res.id_name,
                cur_node->user_res.phone,
                cur_node->user_res.venue_Type,
                cur_node->user_res.num);
            //打印时间字段
            printf("%s/", cur_node->user_res.year);
            printf("%s/", cur_node->user_res.month);
            printf("%s/", cur_node->user_res.day);
            printf("%s-", cur_node->user_res.start_time);
            printf("%s\t", cur_node->user_res.end_time);
            ////打印状态
            //printf("到场状态：%d\t", cur_node->user_res.flag);
            //打印该预约信息编号
            printf("该预约信息的编号为：%d\n", cur_node->user_res.res_count);
        }
        cur_node = cur_node->next;
    }
}
// 仅预约场地
void only_type_reserved(System* sys, int facility_manager_id) {
    print_only_type(&sys->reservation_link);
    printf("请输入对应的预订ID：");
    while (true) {
        int res_id = 0;
        scanf_s("%d", &res_id);
        clear_input_buffer();
        User_res* user_res = find_res(&sys->reservation_link, res_id);
        if (user_res == NULL) {
            printf("无此ID，请重新输入:");
        }
        else {
            // 打印场地负责人管理的对应类型的场地
            int index = 0;
            FNode* f_node = sys->facility_link.head;
            while (f_node != NULL) {
                if (f_node->facility.type == user_res->venue_Type && f_node->facility.manager_id == facility_manager_id) {
                    index = 1;
                    printf("ID: %d, 名称: %s 场地类型：",
                            f_node->facility.id,
                            f_node->facility.name);
                    print_facility_type(f_node->facility.type);
                    printf(" 开放时间: %s, 租金: %.2f\n",
                            f_node->facility.open_time,
                            f_node->facility.price_per_half_hour);
                    printf("08:00   09:00   10:00   11:00   12:00   13:00   14:00\n");
                    for (int j = 0; j < 14; j++) {
                        printf("  %d ", f_node->facility.time_list.head->next->t.day_time[j]);
                    }
                    printf("\n15:00   16:00   17:00   18:00   19:00   20:00   21:00\n");
                    for (int j = 14; j < 28; j++) {
                        printf("  %d ", f_node->facility.time_list.head->next->t.day_time[j]);
                    }
                    printf("\n");
                }
                f_node = f_node->next;
            }
            if (index == 0) {
                printf("该场地负责人不管理该类型的场地,无法分配\n");
            }
            else {
                // 打印 可满足的场地（需要实现）
                printf("该时间段空余的场地ID：");
                int f_id[MAX_FACILITIES] = { 0 };
                int size = 0;
                int year = atoi(user_res->year);
                int month = atoi(user_res->month);
                int day = atoi(user_res->day);
                FacilityManager facility_manager = *find_facility_manager(&sys->facility_manager_link, facility_manager_id);
                for (int i = 0; i < facility_manager.num_facilities; i++) {
                    if (judge_facility_free(facility_manager.facilities[i], year, month, day, user_res->start_time, user_res->end_time)) {
                        printf("%d  ", facility_manager.facilities[i]->id);
                        f_id[size] = facility_manager.facilities[i]->id;
                        size++;
                    }
                }
                printf("\n");
                // 分配
                int facility_id;
                while (true) {
                    int index2 = 0;
                    printf("请输入分配的场地ID：");
                    scanf_s("%d", &facility_id);
                    clear_input_buffer();
                    // id 满足要求（需要查找）
                    for (int i = 0; i < size; i++) {
                        if (facility_id == f_id[i]) {
                            index2 = 1;
                            break;
                        }
                        else {
                            continue;
                        }
                    }
                    if (index2 == 1) {
                        break;
                    }
                    else {
                        printf("输入场地ID不合法\n");
                    }
                }
                    user_res->num = facility_id;
                    user_res->flag = 1;
                    save_res_data(&sys->reservation_link);  //保存修改
                    // 场地人数+1（需要实现）
                    Facility* facility = find_facility(&sys->facility_link, user_res->num);
                    add_facility_people(facility, year, month, day, user_res->start_time, user_res->end_time);
                    // 创建使用记录
                    User_node* user_node = find_phone_data(sys->user_link, user_res->phone);
                    Record record = create_record(&user_node->user, facility, user_res->year,
                        user_res->month, user_res->day, user_res->start_time, user_res->end_time, false);
                    add_record(&sys->record_link, &record);
                    write_record_file(sys, "..\\src\\record.txt");    //保存
                    printf("登记成功\n");
            }

            system("pause");
            break;
        }
    }
}


